package www.ym.com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.BaseContext;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.entity.AddressBook;
import www.ym.com.service.AddressBookService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

//    @Autowired
//    private AddressBookService addressBookService;
//
//    /**
//     * 新增地址
//     *
//     * @param addressBook 地址信息
//     * @return
//     */
//    @PostMapping
//    public R<AddressBook> addAddressBook(@RequestBody AddressBook addressBook) {
//        //userId赋值
//        addressBook.setUserId(BaseContext.getCurrentId());
//        addressBookService.save(addressBook);
//        return R.success(addressBook);
//    }
//
//    /**
//     * 查询地址列表
//     *
//     * @param addressBook
//     * @return
//     */
//    @GetMapping("/list")
//    public R<List<AddressBook>> list(AddressBook addressBook) {
//
//        addressBook.setUserId(BaseContext.getCurrentId());
//        //查询数据库
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
//        queryWrapper.orderByDesc(AddressBook::getIsDefault).orderByDesc(AddressBook::getUpdateTime);
//        List<AddressBook> list = addressBookService.list(queryWrapper);
//        return R.success(list);
//    }
//
//    /**
//     * 设置为默认地址
//     *
//     * @param addressBook
//     */
//    @PutMapping("/default")
//    public R<AddressBook> defaultAddress(@RequestBody AddressBook addressBook) {
//
//        //将当前默认地址改为否
//        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
//        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
//        queryWrapper.set(AddressBook::getIsDefault, 0);
//        addressBookService.update(queryWrapper);
//        //其他地址改为默认地址
//        addressBook.setIsDefault(1);
//        addressBookService.updateById(addressBook);
//        return R.success(addressBook);
//    }
//
//
//    /**
//     * 根据id查询地址信息
//     *
//     * @param
//     * @return
//     */
//    @GetMapping("/{id}")
//    public R<AddressBook> getByidAddressBook(@PathVariable Long id) {
//        AddressBook addressBook = addressBookService.getById(id);
//        if (addressBook != null) {
//            return R.success(addressBook);
//        } else {
//            return R.error("没有找到该对象");
//        }
//    }
//
//
//    /**
//     * 修改地址
//     *
//     * @param addressBook
//     * @return
//     */
//    @PutMapping
//    public R<String> putIdAddressBook(@RequestBody AddressBook addressBook) {
//        addressBookService.updateById(addressBook);
//        return R.success("修改数据成功");
//    }
//
//    @DeleteMapping
//    public R<String> byIdDelete(@RequestParam("ids") Long id) {
//        //查询数据库，默认地址簿不能删除
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(id != null, AddressBook::getId, id);
//        queryWrapper.eq(AddressBook::getIsDefault, 1);
//        int count = addressBookService.count(queryWrapper);
//        if (count > 0) {
//            throw new CustomException("默认地址簿无法删除");
//        }
//        addressBookService.removeById(id);
//        return R.success("删除成功");
//    }
//
//    /**
//     * 默认地址
//     */
//    @GetMapping("/default")
//    public R<AddressBook> defaultAddress(){
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
//        queryWrapper.eq(AddressBook::getIsDefault, 1);
//        AddressBook addressBook = addressBookService.getOne(queryWrapper);
//        return R.success(addressBook);
//    }

}
